package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysDictData;
import com.blb.zy_community.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController
public class SysDictDataController {

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 字典数据分页数据
     * @param current
     * @param size
     * @param dictType
     * @param dictLabel
     * @param status
     * @return
     */
    @GetMapping("/sysDictData/Page")
    public ResponseResult<Page<SysDictData>> getSysDictDataPage(
            @RequestParam(required = false, defaultValue = "1") Long current,
            @RequestParam(required = false, defaultValue = "5") Long size,
            @RequestParam(required = false) String dictType,
            @RequestParam(required = false) Long dictLabel,
            @RequestParam(required = false) String status
    ) {
        Page<SysDictData> sysDictDataPage = sysDictDataService.getSysDictDataPage(new Page<>(current, size), dictType, dictLabel, status);
        return ResponseResult.ok(sysDictDataPage);
    }

    /**
     * 添加字典数据
     * @param sysDictData
     * @return
     */
    @PostMapping("/sysDictData")
    public ResponseResult<SysDictData> addSysDictData(@RequestBody SysDictData sysDictData) {
        sysDictDataService.save(sysDictData);
        return ResponseResult.ok(sysDictData);
    }

    /**
     * 修改字典数据
     * @param sysDictData
     * @return
     */
    @PutMapping("/sysDictData")
    public ResponseResult<SysDictData> updateSysDictData(@RequestBody SysDictData sysDictData) {
        sysDictDataService.updateById(sysDictData);
        return ResponseResult.ok(sysDictData);
    }

    /**
     * 根据id删除字典数据
     * @param dictCode
     * @return
     */
    @DeleteMapping("/sysDictData/{dictCode}")
    public ResponseResult<SysDictData> deleteSysDictData(@PathVariable("dictCode") Long dictCode){
        sysDictDataService.removeById(dictCode);
        return ResponseResult.ok(dictCode);
    }

    /**
     * 根据系统字典类型的名称获取系统字典数据的集合
     * @param sysDictTypeName
     * @return
     */
    @GetMapping("/sysDictData/getBySysDictTypeName/{sysDictTypeName}")
    public ResponseResult<List<SysDictData>> getSysDictDataBySysDictTypeName(@PathVariable("sysDictTypeName") String sysDictTypeName){
        List<SysDictData> sysDictDataList= sysDictDataService.getSysDictDataBySysDictTypeName(sysDictTypeName);
        return ResponseResult.ok(sysDictDataList);
    }

}
